package com.vtc.session3;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class EmployeeListActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		
		ArrayList<Employee> arr = new ArrayList<Employee>();
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen B", "Male", 20));
		arr.add(new Employee("Vu Lam", "Male", 20));
		arr.add(new Employee("Hong Ngan", "FeMale", 20));
		arr.add(new Employee("Nguyen Tuan", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen B", "Male", 20));
		arr.add(new Employee("Vu Lam", "Male", 20));
		arr.add(new Employee("Hong Ngan", "FeMale", 20));
		arr.add(new Employee("Nguyen Tuan", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen B", "Male", 20));
		arr.add(new Employee("Vu Lam", "Male", 20));
		arr.add(new Employee("Hong Ngan", "FeMale", 20));
		arr.add(new Employee("Nguyen Tuan", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen B", "Male", 20));
		arr.add(new Employee("Vu Lam", "Male", 20));
		arr.add(new Employee("Hong Ngan", "FeMale", 20));
		arr.add(new Employee("Nguyen Tuan", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		
		TableLayout layout = new TableLayout(this);
		android.widget.TableLayout.LayoutParams para = new android.widget.TableLayout.LayoutParams();
		
		layout.setStretchAllColumns(true);
		
		TableRow row = new TableRow(this);
		row.setBackgroundColor(Color.BLUE);		
		LayoutParams paras = new LayoutParams();
		paras.height = 30;
		
		
		TextView tvName = new TextView(this);
		tvName.setText("Full Name");
		tvName.setTextColor(Color.WHITE);
		tvName.setLayoutParams(paras);
		tvName.setGravity(Gravity.CENTER_VERTICAL);
		
		TextView tvGender= new TextView(this);
		tvGender.setText("Gender");
		tvGender.setTextColor(Color.WHITE);
		tvGender.setLayoutParams(paras);
		tvGender.setGravity(Gravity.CENTER_VERTICAL);
		
		TextView tvAge = new TextView(this);
		tvAge.setText("Age");
		tvAge.setTextColor(Color.WHITE);
		tvAge.setLayoutParams(paras);
		tvAge.setGravity(Gravity.CENTER_VERTICAL);
		row.addView(tvName);
		row.addView(tvGender);
		row.addView(tvAge);
		layout.addView(row);
		int i=0;
		for(Employee e:arr){
			
			TableRow row1 = new TableRow(this);
			
			TextView tvName1 = new TextView(this);
			tvName1.setText(e.getName());
			tvName1.setLayoutParams(paras);
			
			TextView tvGender1= new TextView(this);
			tvGender1.setText(e.getGender());
			tvGender1.setLayoutParams(paras);
			
			TextView tvAge1 = new TextView(this);
			tvAge1.setText(e.getAge()+"");
			tvAge1.setLayoutParams(paras);
			
			row1.addView(tvName1);
			row1.addView(tvGender1);
			row1.addView(tvAge1);
			if(++i%2==0){
				row1.setBackgroundColor(Color.WHITE);
			}
			else{
				row1.setBackgroundColor(Color.GRAY);
			}
			// dang ky su kien clikc vao tung rong
			row1.setOnClickListener(this);
			layout.addView(row1);
		}		
		this.setContentView(layout);
	}

	@Override
	public void onClick(View v) {		
		TableRow row = (TableRow) v;
		TextView tv1 = (TextView) row.getChildAt(0);
		TextView tv2 = (TextView) row.getChildAt(1);
		TextView tv3 = (TextView) row.getChildAt(2);
		
		String data = tv1.getText().toString()+">>"+tv2.getText().toString()+">>"+tv3.getText().toString();
		Toast.makeText(this, data, Toast.LENGTH_LONG).show();
	}
}
